home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 July / macformat-026.iso / mac / Shareware City / Developers / berkeleydb1.73 / Berkeley_db / hash / hash_buf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-27  |  8.9 KB  |  349 lines  |  [TEXT/MPS ]

  1. /*-
  2.  * Copyright (c) 1990, 1993
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Margo Seltzer.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #if defined(LIBC_SCCS) && !defined(lint)
  38. static char sccsid[] = "@(#)hash_buf.c    8.1 (Berkeley) 6/4/93";
  39. #endif /* LIBC_SCCS and not lint */
  40.  
  41. #if defined(macintosh) && (defined(powerc) || defined(__powerc))
  42. #include "OurMalloc.h"
  43. #endif
  44.  
  45. /*
  46.  * PACKAGE: hash
  47.  *
  48.  * DESCRIPTION:
  49.  *    Contains buffer management
  50.  *
  51.  * ROUTINES:
  52.  * External
  53.  *    __buf_init
  54.  *    __get_buf
  55.  *    __buf_free
  56.  *    __reclaim_buf
  57.  * Internal
  58.  *    newbuf
  59.  */
  60.  
  61. #include <sys/param.h>
  62.  
  63. #include <errno.h>
  64. #include <stdio.h>
  65. #include <stdlib.h>
  66. #ifdef DEBUG
  67. #include <assert.h>
  68. #endif
  69.  
  70. #include <db.h>
  71. #include "hash.h"
  72. #include "page.h"
  73. #include "extern.h"
  74.  
  75. static BUFHEAD *newbuf __P((HTAB *, u_int, BUFHEAD *));
  76.  
  77. /* Unlink B from its place in the lru */
  78. #define BUF_REMOVE(B) { \
  79.     (B)->prev->next = (B)->next; \
  80.     (B)->next->prev = (B)->prev; \
  81. }
  82.  
  83. /* Insert B after P */
  84. #define BUF_INSERT(B, P) { \
  85.     (B)->next = (P)->next; \
  86.     (B)->prev = (P); \
  87.     (P)->next = (B); \
  88.     (B)->next->prev = (B); \
  89. }
  90.  
  91. #define    MRU    hashp->bufhead.next
  92. #define    LRU    hashp->bufhead.prev
  93.  
  94. #define MRU_INSERT(B)    BUF_INSERT((B), &hashp->bufhead)
  95. #define LRU_INSERT(B)    BUF_INSERT((B), LRU)
  96.  
  97. /*
  98.  * We are looking for a buffer with address "addr".  If prev_bp is NULL, then
  99.  * address is a bucket index.  If prev_bp is not NULL, then it points to the
  100.  * page previous to an overflow page that we are trying to find.
  101.  *
  102.  * CAVEAT:  The buffer header accessed via prev_bp's ovfl field may no longer
  103.  * be valid.  Therefore, you must always verify that its address matches the
  104.  * address you are seeking.
  105.  */
  106. extern BUFHEAD *
  107. __get_buf(hashp, addr, prev_bp, newpage)
  108.     HTAB *hashp;
  109.     u_int addr;
  110.     BUFHEAD *prev_bp;
  111.     int newpage;    /* If prev_bp set, indicates a new overflow page. */
  112. {
  113.     register BUFHEAD *bp;
  114.     register u_int is_disk_mask;
  115.     register int is_disk, segment_ndx;
  116.     SEGMENT segp;
  117.  
  118.     is_disk = 0;
  119.     is_disk_mask = 0;
  120.     if (prev_bp) {
  121.         bp = prev_bp->ovfl;
  122.         if (!bp || (bp->addr != addr))
  123.             bp = NULL;
  124.         if (!newpage)
  125.             is_disk = BUF_DISK;
  126.     } else {
  127.         /* Grab buffer out of directory */
  128.         segment_ndx = addr & (hashp->SGSIZE - 1);
  129.  
  130.         /* valid segment ensured by __call_hash() */
  131.         segp = hashp->dir[addr >> hashp->SSHIFT];
  132. #ifdef DEBUG
  133.         assert(segp != NULL);
  134. #endif
  135.         bp = PTROF(segp[segment_ndx]);
  136.         is_disk_mask = ISDISK(segp[segment_ndx]);
  137.         is_disk = is_disk_mask || !hashp->new_file;
  138.     }
  139.  
  140.     if (!bp) {
  141.         bp = newbuf(hashp, addr, prev_bp);
  142.         if (!bp ||
  143.             __get_page(hashp, bp->page, addr, !prev_bp, is_disk, 0))
  144.             return (NULL);
  145.         if (!prev_bp)
  146.             segp[segment_ndx] =
  147.                 (BUFHEAD *)((u_int)bp | is_disk_mask);
  148.     } else {
  149.         BUF_REMOVE(bp);
  150.         MRU_INSERT(bp);
  151.     }
  152.     return (bp);
  153. }
  154.  
  155. /*
  156.  * We need a buffer for this page. Either allocate one, or evict a resident
  157.  * one (if we have as many buffers as we're allowed) and put this one in.
  158.  *
  159.  * If newbuf finds an error (returning NULL), it also sets errno.
  160.  */
  161. static BUFHEAD *
  162. newbuf(hashp, addr, prev_bp)
  163.     HTAB *hashp;
  164.     u_int addr;
  165.     BUFHEAD *prev_bp;
  166. {
  167.     register BUFHEAD *bp;        /* The buffer we're going to use */
  168.     register BUFHEAD *xbp;        /* Temp pointer */
  169.     register BUFHEAD *next_xbp;
  170.     SEGMENT segp;
  171.     int segment_ndx;
  172.     u_short oaddr, *shortp;
  173.  
  174.     oaddr = 0;
  175.     bp = LRU;
  176.     /*
  177.      * If LRU buffer is pinned, the buffer pool is too small. We need to
  178.      * allocate more buffers.
  179.      */
  180.     if (hashp->nbufs || (bp->flags & BUF_PIN)) {
  181.         /* Allocate a new one */
  182.         bp = malloc(sizeof(struct _bufhead));
  183.         if (!bp || !(bp->page = malloc(hashp->BSIZE)))
  184.             return (NULL);
  185.         if (hashp->nbufs)
  186.             hashp->nbufs--;
  187.     } else {
  188.         /* Kick someone out */
  189.         BUF_REMOVE(bp);
  190.         /*
  191.          * If this is an overflow page with addr 0, it's already been
  192.          * flushed back in an overflow chain and initialized.
  193.          */
  194.         if ((bp->addr != 0) || (bp->flags & BUF_BUCKET)) {
  195.             /*
  196.              * Set oaddr before __put_page so that you get it
  197.              * before bytes are swapped.
  198.              */
  199.             shortp = (u_short *)bp->page;
  200.             if (shortp[0])
  201.                 oaddr = shortp[shortp[0] - 1];
  202.             if ((bp->flags & BUF_MOD) && __put_page(hashp, bp->page,
  203.                 bp->addr, (int)IS_BUCKET(bp->flags), 0))
  204.                 return (NULL);
  205.             /*
  206.              * Update the pointer to this page (i.e. invalidate it).
  207.              *
  208.              * If this is a new file (i.e. we created it at open
  209.              * time), make sure that we mark pages which have been
  210.              * written to disk so we retrieve them from disk later,
  211.              * rather than allocating new pages.
  212.              */
  213.             if (IS_BUCKET(bp->flags)) {
  214.                 segment_ndx = bp->addr & (hashp->SGSIZE - 1);
  215.                 segp = hashp->dir[bp->addr >> hashp->SSHIFT];
  216. #ifdef DEBUG
  217.                 assert(segp != NULL);
  218. #endif
  219.  
  220.                 if (hashp->new_file &&
  221.                     ((bp->flags & BUF_MOD) ||
  222.                     ISDISK(segp[segment_ndx])))
  223.                     segp[segment_ndx] = (BUFHEAD *)BUF_DISK;
  224.                 else
  225.                     segp[segment_ndx] = NULL;
  226.             }
  227.             /*
  228.              * Since overflow pages can only be access by means of
  229.              * their bucket, free overflow pages associated with
  230.              * this bucket.
  231.              */
  232.             for (xbp = bp; xbp->ovfl;) {
  233.                 next_xbp = xbp->ovfl;
  234.                 xbp->ovfl = 0;
  235.                 xbp = next_xbp;
  236.  
  237.                 /* Check that ovfl pointer is up date. */
  238.                 if (IS_BUCKET(xbp->flags) ||
  239.                     (oaddr != xbp->addr))
  240.                     break;
  241.  
  242.                 shortp = (u_short *)xbp->page;
  243.                 if (shortp[0])
  244.                     /* set before __put_page */
  245.                     oaddr = shortp[shortp[0] - 1];
  246.                 if ((xbp->flags & BUF_MOD) && __put_page(hashp,
  247.                     xbp->page, xbp->addr, 0, 0))
  248.                     return (NULL);
  249.                 xbp->addr = 0;
  250.                 xbp->flags = 0;
  251.                 BUF_REMOVE(xbp);
  252.                 LRU_INSERT(xbp);
  253.             }
  254.         }
  255.     }
  256.  
  257.     /* Now assign this buffer */
  258.     bp->addr = addr;
  259. #ifdef DEBUG1
  260.     (void)fprintf(stderr, "NEWBUF1: %d->ovfl was %d is now %d\n",
  261.         bp->addr, (bp->ovfl ? bp->ovfl->addr : 0), 0);
  262. #endif
  263.     bp->ovfl = NULL;
  264.     if (prev_bp) {
  265.         /*
  266.          * If prev_bp is set, this is an overflow page, hook it in to
  267.          * the buffer overflow links.
  268.          */
  269. #ifdef DEBUG1
  270.         (void)fprintf(stderr, "NEWBUF2: %d->ovfl was %d is now %d\n",
  271.             prev_bp->addr, (prev_bp->ovfl ? bp->ovfl->addr : 0),
  272.             (bp ? bp->addr : 0));
  273. #endif
  274.         prev_bp->ovfl = bp;
  275.         bp->flags = 0;
  276.     } else
  277.         bp->flags = BUF_BUCKET;
  278.     MRU_INSERT(bp);
  279.     return (bp);
  280. }
  281.  
  282. extern void
  283. __buf_init(hashp, nbytes)
  284.     HTAB *hashp;
  285.     int nbytes;
  286. {
  287.     BUFHEAD *bfp;
  288.     int npages;
  289.  
  290.     bfp = &(hashp->bufhead);
  291.     npages = (nbytes + hashp->BSIZE - 1) >> hashp->BSHIFT;
  292.     npages = MAX(npages, MIN_BUFFERS);
  293.  
  294.     hashp->nbufs = npages;
  295.     bfp->next = bfp;
  296.     bfp->prev = bfp;
  297.     /*
  298.      * This space is calloc'd so these are already null.
  299.      *
  300.      * bfp->ovfl = NULL;
  301.      * bfp->flags = 0;
  302.      * bfp->page = NULL;
  303.      * bfp->addr = 0;
  304.      */
  305. }
  306.  
  307. extern int
  308. __buf_free(hashp, do_free, to_disk)
  309.     HTAB *hashp;
  310.     int do_free, to_disk;
  311. {
  312.     BUFHEAD *bp;
  313.  
  314.     /* Need to make sure that buffer manager has been initialized */
  315.     if (!LRU)
  316.         return (0);
  317.     for (bp = LRU; bp != &hashp->bufhead;) {
  318.         /* Check that the buffer is valid */
  319.         if (bp->addr || IS_BUCKET(bp->flags)) {
  320.             if (to_disk && (bp->flags & BUF_MOD) &&
  321.                 __put_page(hashp, bp->page,
  322.                 bp->addr, IS_BUCKET(bp->flags), 0))
  323.                 return (-1);
  324.         }
  325.         /* Check if we are freeing stuff */
  326.         if (do_free) {
  327.             if (bp->page)
  328.                 free(bp->page);
  329.             BUF_REMOVE(bp);
  330.             free(bp);
  331.             bp = LRU;
  332.         } else
  333.             bp = bp->prev;
  334.     }
  335.     return (0);
  336. }
  337.  
  338. extern void
  339. __reclaim_buf(hashp, bp)
  340.     HTAB *hashp;
  341.     BUFHEAD *bp;
  342. {
  343.     bp->ovfl = 0;
  344.     bp->addr = 0;
  345.     bp->flags = 0;
  346.     BUF_REMOVE(bp);
  347.     LRU_INSERT(bp);
  348. }
  349.